package com.metaera.app.controller.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.metaera.app.R;
import com.metaera.app.controller.view.core.INetworkRequestView;
import com.metaera.app.data.model.HomeTopNewsResponse;
import com.metaera.app.data.model.HotColumnResponse;
import com.metaera.app.ui.activities.ChoiceTopicActivity;
import com.metaera.app.ui.activities.HotColumnListActivity;
import com.metaera.app.ui.activities.InvestFinanceActivity;
import com.metaera.app.ui.activities.MorningPostActivity;
import com.metaera.app.ui.activities.RecommendNewsListActivity;
import com.metaera.app.ui.activities.TodayEventActivity;
import com.metaera.app.ui.adapters.HomeBannerAdapter;
import com.metaera.app.ui.adapters.HomeHotNewsAdapter;
import com.metaera.app.ui.adapters.HomeRecommendNewsAdapter;
import com.metaera.app.ui.adapters.HotColumnAdapter;
import com.youth.banner.Banner;
import com.youth.banner.indicator.RectangleIndicator;

import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.RecyclerView;

public class HomeTopNewsView implements INetworkRequestView<HomeTopNewsResponse> {

//    return R.layout.include_home_top_news;

    private final View mRootView;

    private final Banner<HomeTopNewsResponse.BnnerLists, HomeBannerAdapter> mBanner;

    private final View mLayoutRecommend, mLayoutHotNews, mLayoutHotColumn;

    private final RecyclerView mRecyclerRecommend, mRecyclerHotNews, mRecyclerHotColumn;


    public HomeTopNewsView(ViewGroup parent, Fragment fragment) {
        Context context = fragment.getContext();
        mRootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.include_home_top_news, parent, false);

        // find
        TextView tvRecommendMore = mRootView.findViewById(R.id.tv_recommend_more); // 推荐，更多
        TextView tvHotColumn = mRootView.findViewById(R.id.tv_hot_column); // 推荐，更多
        mRecyclerRecommend = mRootView.findViewById(R.id.recycler_recommend); // 主编推荐
        mRecyclerHotNews = mRootView.findViewById(R.id.recycler_hot_news); // 热点新闻
        mRecyclerHotColumn = mRootView.findViewById(R.id.recycler_hot_column); // 热门专栏

        mLayoutRecommend = mRootView.findViewById(R.id.layout_recommend);
        mLayoutHotNews = mRootView.findViewById(R.id.layout_hot_news);
        mLayoutHotColumn = mRootView.findViewById(R.id.layout_hot_column);

        // 初始化banner
        mBanner = mRootView.findViewById(R.id.banner);
        mBanner.addBannerLifecycleObserver(fragment);

        if (context != null) {
            mBanner.setIndicator(new RectangleIndicator(context));

            // listener
            mRootView.findViewById(R.id.view_morning_post) // 新闻早八点
                    .setOnClickListener(v -> context.startActivity(new Intent(context, MorningPostActivity.class)));

            mRootView.findViewById(R.id.view_invest_finance) // 投融资
                    .setOnClickListener(v -> context.startActivity(new Intent(context, InvestFinanceActivity.class)));

            mRootView.findViewById(R.id.view_today_event) // 今日大事件
                    .setOnClickListener(v -> context.startActivity(new Intent(context, TodayEventActivity.class)));

            mRootView.findViewById(R.id.view_choice_topic) // 精选专题
                    .setOnClickListener(v -> context.startActivity(new Intent(context, ChoiceTopicActivity.class)));

            tvRecommendMore.setOnClickListener(v -> // 主编推荐
                    context.startActivity(new Intent(context, RecommendNewsListActivity.class)));

            tvHotColumn.setOnClickListener(v -> // 更多热门专栏
                    context.startActivity(new Intent(context, HotColumnListActivity.class)));
        }

    }

    @Override
    public void successful(HomeTopNewsResponse data) {
        List<HotColumnResponse> columnLists = data.getColumnLists();
        List<HomeTopNewsResponse.TopLists> topLists = data.getTopLists();
        List<HomeTopNewsResponse.RecommendLists> recommendLists = data.getRecommendLists();

        mLayoutRecommend.setVisibility(recommendLists != null && recommendLists.size() > 1 ? View.VISIBLE : View.GONE);
        mLayoutHotNews.setVisibility(topLists != null && topLists.size() > 1 ? View.VISIBLE : View.GONE);
        mLayoutHotColumn.setVisibility(columnLists != null && columnLists.size() > 1 ? View.VISIBLE : View.GONE);

        mBanner.setAdapter(new HomeBannerAdapter(data.getBnnerLists()));

        // 主编推荐
        if (recommendLists != null && recommendLists.size() > 2) {
            recommendLists = recommendLists.subList(0, 2);
            mRecyclerRecommend.setAdapter(new HomeRecommendNewsAdapter(recommendLists));
        }

        // 热点新闻
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(mRecyclerHotNews.getContext(), DividerItemDecoration.VERTICAL);
        Drawable drawable = ContextCompat.getDrawable(mRecyclerHotNews.getContext(), R.drawable.shape_item_divider);
        dividerItemDecoration.setDrawable(drawable);
        mRecyclerHotNews.addItemDecoration(dividerItemDecoration);
        mRecyclerHotNews.setAdapter(new HomeHotNewsAdapter(topLists));

        // 热门专栏
        mRecyclerHotColumn.setAdapter(new HotColumnAdapter(columnLists));
    }

    public View getView() {
        return mRootView;
    }

}
